package cn.bfay.util;

import org.apache.commons.io.IOUtils;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.geom.QuadCurve2D;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * 验证码工具类.
 *
 * @author wangjiannan
 * /@GetMapping("/captcha.jpg")
 * public void captcha() {
 * captchaService.generate();
 * }
 * ...
 * public class CaptchaServiceImpl implements CaptchaService {
 * private static final String COOKIE_KEY = "bfay-captcha";
 * private static final int MAX_AGE = -1; // cookie超时默认为session会话状态
 * private final HttpServletRequest request;
 * private final HttpServletResponse response;
 * \@Override public void generate() {
 * // 先检查cookie的uuid是否存在
 * String cookieValue = WebUtils.getCookieValue(request, COOKIE_KEY);
 * boolean hasCookie = true;
 * if (StringUtils.isBlank(cookieValue)) {
 * hasCookie = false;
 * cookieValue = StringUtils.getUUId();
 * }
 * String captchaCode = CaptchaUtils.generateCode().toUpperCase();// 转成大写重要
 * // 不存在cookie时设置cookie
 * if (!hasCookie) {
 * WebUtils.setCookie(response, COOKIE_KEY, cookieValue, MAX_AGE);
 * }
 * // 生成验证码
 * CaptchaUtils.generate(response, captchaCode);
 * String redisKey = RedisKeyBuilder.generateRedisKey(RedisKeyTemplate.CAPTCHA_TEMPLATE, cookieValue);
 * RedisUtils.setValue(redisKey, captchaCode, RedisTime.HNOUR);
 * }
 * \@Override public void validate(String input) {
 * String cookieValue = WebUtils.getCookieValue(request, COOKIE_KEY);
 * if (StringUtils.isBlank(cookieValue)) {
 * throw new BizException(RestBusinessError.LOGIN_ERROR_CAPTCHA);
 * }
 * String redisKey = RedisKeyBuilder.generateRedisKey(RedisKeyTemplate.CAPTCHA_TEMPLATE, cookieValue);
 * String captchaCode = RedisUtils.getValue(redisKey);
 * if (StringUtils.isBlank(captchaCode)) {
 * throw new BizException(RestBusinessError.LOGIN_ERROR_CAPTCHA);
 * }
 * if (!input.equalsIgnoreCase(captchaCode)) {
 * throw new BizException(RestBusinessError.LOGIN_ERROR_CAPTCHA);
 * }
 * RedisUtils.delete(redisKey);
 * WebUtils.removeCookie(response, COOKIE_KEY);
 * }
 * }
 */
public class CaptchaUtils {


    // 生成随机类
    private static final Random RANDOM = new Random();
    // 默认的验证码大小
    private static final int WIDTH = 108, HEIGHT = 40, CODE_SIZE = 4;
    // 验证码随机字符数组
    private static final char[] charArray = "3456789ABCDEFGHJKMNPQRSTUVWXY".toCharArray();
    // 验证码字体
    private static final Font[] RANDOM_FONT = new Font[]{new Font("nyala", Font.BOLD, 38), new Font("Arial", Font.BOLD, 32),
        new Font("Bell MT", Font.BOLD, 32), new Font("Credit valley", Font.BOLD, 34), new Font("Impact", Font.BOLD, 32),
        new Font(Font.MONOSPACED, Font.BOLD, 40)};

    /**
     * 生成验证码字符串.
     *
     * @return string
     */
    public static String generateCode() {
        int count = CODE_SIZE;
        char[] buffer = new char[count];
        for (int i = 0; i < count; i++) {
            buffer[i] = charArray[RANDOM.nextInt(charArray.length)];
        }
        return new String(buffer);
    }

    /**
     * 生成验证码.
     */
    public static void generate(HttpServletResponse response, String code) {
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");

        ServletOutputStream sos = null;
        try {
            drawGraphic(image, code);
            sos = response.getOutputStream();
            ImageIO.write(image, "JPEG", sos);
            sos.flush();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            IOUtils.closeQuietly(sos);
        }
    }

    private static void drawGraphic(BufferedImage image, String code) {
        // 获取图形上下文
        Graphics2D g = image.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
        // 图形抗锯齿
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        // 字体抗锯齿
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        // 设定背景色，淡色
        g.setColor(getRandColor(210, 250));
        g.fillRect(0, 0, WIDTH, HEIGHT);

        // 画小字符背景
        Color color = null;
        for (int i = 0; i < 20; i++) {
            color = getRandColor(120, 200);
            g.setColor(color);
            String rand = String.valueOf(charArray[RANDOM.nextInt(charArray.length)]);
            g.drawString(rand, RANDOM.nextInt(WIDTH), RANDOM.nextInt(HEIGHT));
            color = null;
        }
        // 取随机产生的认证码(4位数字)
        char[] buffer = code.toCharArray();
        for (int i = 0; i < buffer.length; i++) {
            char _code = buffer[i];
            // 旋转度数 最好小于45度
            int degree = RANDOM.nextInt(28);
            if (i % 2 == 0) {
                degree = degree * (-1);
            }
            // 定义坐标
            int x = 22 * i, y = 21;
            // 旋转区域
            g.rotate(Math.toRadians(degree), x, y);
            // 设定字体颜色
            color = getRandColor(20, 130);
            g.setColor(color);
            // 设定字体，每次随机
            g.setFont(RANDOM_FONT[RANDOM.nextInt(RANDOM_FONT.length)]);
            // 将认证码显示到图象中
            g.drawString("" + _code, x + 8, y + 10);
            // 旋转之后，必须旋转回来
            g.rotate(-Math.toRadians(degree), x, y);
        }
        // 图片中间曲线，使用上面缓存的color
        g.setColor(color);
        // width是线宽,float型
        BasicStroke bs = new BasicStroke(3);
        g.setStroke(bs);
        // 画出曲线
        QuadCurve2D.Double curve = new QuadCurve2D.Double(0d, RANDOM.nextInt(HEIGHT - 8) + 4, WIDTH / 2, HEIGHT / 2, WIDTH, RANDOM.nextInt(HEIGHT - 8) + 4);
        g.draw(curve);
        // 销毁图像
        g.dispose();
    }

    // 给定范围获得随机颜色
    private static Color getRandColor(int fc, int bc) {
        if (fc > 255) {
            fc = 255;
        }
        if (bc > 255) {
            bc = 255;
        }
        int r = fc + RANDOM.nextInt(bc - fc);
        int g = fc + RANDOM.nextInt(bc - fc);
        int b = fc + RANDOM.nextInt(bc - fc);
        return new Color(r, g, b);
    }
}
